<template>
  <div class="decoder-container">
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="header-content">
        <div class="logo-section">
          <div class="logo">
            <ScanOutlined class="logo-icon" />
          </div>
          <div class="title-section">
            <h1 class="page-title">蒜苔解码器</h1>
            <p class="page-subtitle">快速识别和解析二维码内容</p>
          </div>
        </div>
        <div class="header-actions">
          <a-button @click="goBack" class="back-btn">
            <template #icon>
              <ArrowLeftOutlined />
            </template>
            返回
          </a-button>
        </div>
      </div>
    </div>

    <div class="main-content">
      <!-- 左侧：功能操作区 -->
      <div class="left-panel">
        <!-- 输入方式选择 -->
        <div class="input-method-section">
          <h2 class="section-title">
            <span class="title-icon">📷</span>
            选择输入方式
          </h2>
          <a-tabs v-model:activeKey="activeMethod" @change="handleMethodChange">
            <a-tab-pane key="upload" tab="图片上传">
              <ImageUploader 
                @decode-success="handleDecodeSuccess"
                @decode-error="handleDecodeError"
              />
            </a-tab-pane>
            <a-tab-pane key="camera" tab="摄像头扫描">
              <CameraScanner 
                @decode-success="handleDecodeSuccess"
                @decode-error="handleDecodeError"
              />
            </a-tab-pane>
          </a-tabs>
        </div>
      </div>

      <!-- 右侧：结果展示区 -->
      <div class="right-panel">
        <ResultDisplay 
          :result="currentResult"
          :loading="isDecoding"
        />
      </div>
    </div>

    <!-- 历史记录区域 -->
    <div class="history-section">
      <HistoryList 
        :history="decodeHistory"
        @select-history="handleSelectHistory"
        @clear-history="handleClearHistory"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import {
  ScanOutlined,
  ArrowLeftOutlined
} from '@ant-design/icons-vue'
import ImageUploader from './components/ImageUploader.vue'
import CameraScanner from './components/CameraScanner.vue'
import ResultDisplay from './components/ResultDisplay.vue'
import HistoryList from './components/HistoryList.vue'
import { DecodeResult } from './utils/qrDecoder'
import { getHistory, saveDecodeResult, clearHistory } from './utils/storage'

const router = useRouter()

// 响应式数据
const activeMethod = ref('upload')
const currentResult = ref<DecodeResult | null>(null)
const isDecoding = ref(false)
const decodeHistory = ref<DecodeResult[]>([])

// 页面初始化
onMounted(() => {
  loadHistory()
})

// 加载历史记录
const loadHistory = () => {
  decodeHistory.value = getHistory()
}

// 处理输入方式切换
const handleMethodChange = (key: string) => {
  console.log('切换输入方式:', key)
}

// 处理解码成功
const handleDecodeSuccess = (result: DecodeResult) => {
  console.log('解码成功:', result)
  currentResult.value = result
  isDecoding.value = false
  
  // 保存到历史记录
  saveDecodeResult(result)
  loadHistory()
  
  message.success('二维码解码成功！')
}

// 处理解码错误
const handleDecodeError = (error: string) => {
  console.error('解码失败:', error)
  isDecoding.value = false
  message.error(error || '二维码解码失败')
}

// 选择历史记录
const handleSelectHistory = (result: DecodeResult) => {
  currentResult.value = result
}

// 清空历史记录
const handleClearHistory = () => {
  clearHistory()
  loadHistory()
  message.success('历史记录已清空')
}

// 返回上一页
const goBack = () => {
  router.go(-1)
}
</script>

<style scoped>
.decoder-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.page-header {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.logo-section {
  display: flex;
  align-items: center;
  gap: 16px;
}

.logo {
  width: 48px;
  height: 48px;
  background: linear-gradient(135deg, #667eea, #764ba2);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}

.logo-icon {
  font-size: 24px;
  color: white;
}

.title-section {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.page-title {
  font-size: 28px;
  font-weight: 700;
  margin: 0;
  background: linear-gradient(135deg, #667eea, #764ba2);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.page-subtitle {
  font-size: 14px;
  color: #8c8c8c;
  margin: 0;
}

.header-actions {
  display: flex;
  gap: 12px;
}

.back-btn {
  height: 40px;
  border-radius: 8px;
  border: 1px solid #d9d9d9;
  background: white;
  transition: all 0.3s ease;
}

.back-btn:hover {
  border-color: #667eea;
  color: #667eea;
}

.main-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 32px 20px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 32px;
  min-height: calc(100vh - 200px);
}

.left-panel,
.right-panel {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 16px;
  padding: 24px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.input-method-section {
  height: 100%;
}

.section-title {
  font-size: 20px;
  font-weight: 600;
  margin: 0 0 20px 0;
  display: flex;
  align-items: center;
  gap: 8px;
  color: #1a1a1a;
}

.title-icon {
  font-size: 18px;
}

.history-section {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px 32px;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .main-content {
    grid-template-columns: 1fr;
    gap: 24px;
  }
}

@media (max-width: 768px) {
  .decoder-container {
    padding: 0;
  }
  
  .header-content {
    flex-direction: column;
    gap: 16px;
    text-align: center;
    padding: 20px;
  }
  
  .logo-section {
    flex-direction: column;
    gap: 12px;
  }
  
  .page-title {
    font-size: 24px;
  }
  
  .page-subtitle {
    font-size: 14px;
  }
  
  .main-content {
    padding: 20px;
  }
}
</style>